/**
 * Created by shenyinlin on 2018/6/24.
 */

//排行榜页面

import {Sprite} from "../base/Sprite.js";
import {DataStore} from "../base/DataStore.js";
export class PaiHangPage extends Sprite {

    constructor() {

        const image = Sprite.getImage('paiHangPage');

        super(image, 0, 0,
            image.width, image.height,
            0, 0,
            image.width, image.height);
        this.image = image;

    }
    //这个方法只是 为了达到效果， 真的不忍直视...
    drawRow(topList){
        let index = 1;
        topList.forEach(function(obj){
                console.log(obj);
                let ctx = DataStore.getInstance().ctx;
            ctx.font = '21px Arial';
            ctx.fillStyle = 'black';
            ctx.fillText(
                index,
                (DataStore.getInstance().canvas.width ) / 6.65,
                (DataStore.getInstance().canvas.height) / 6.80,
                1000
            );
            ctx.fillText(
                obj.nickname,
                (DataStore.getInstance().canvas.width ) / 2.1,
                (DataStore.getInstance().canvas.height) / 6.80,
                1000
            );
            // const rowImg = wx.createImage();
            // console.log('obj.avatar='+obj.avatar);
            // rowImg.src = obj.avatar;

            //TODO FIX ME  AVATAR CAN'T LOAD IN THIS CANVAS
            // wx.getImageInfo({
            //     src: obj.avatar
            // }).then(res => {
            //     ctx.drawImage(res.path,
            //         0, 0, 50, 50,
            //         (DataStore.getInstance().canvas.height) / 6.80,
            //         (DataStore.getInstance().canvas.width ) / 3.2,
            //         50,
            //         50)
            //     ctx.draw()
            //
            // })




            let score = obj.score + '分';
            console.log('row score = ' + score)
            ctx.fillText(
                score,
                (DataStore.getInstance().canvas.width ) / 1.2,
                (DataStore.getInstance().canvas.height) / 6.80,
                1000
            );
         })
        index++;
    }



    draw(){
        console.log('渲染了排行榜页面');
        var div_width= DataStore.getInstance().canvas.width;
        var div_height= DataStore.getInstance().canvas.height;
        var old_width= this.image.width;
        var old_height= this.image.height;

        var scale_x=div_width*old_width/old_height;
        var scale_y=div_width*old_height/old_width;



        if(old_height>old_width){
            super.draw(this.image, 0, 0,
                this.image.width, this.image.height,
                0,-(scale_y-div_width)/45,
                div_width,scale_y);
          //  ctx.drawImage(image,0,0,old_width,old_height,0,-(scale_y-div_width)/2,div_width,scale_y);

        }else{
            super.draw(this.image, 0, 0,
                this.image.width, this.image.height,
                -(scale_x-div_width)/2,0,
                scale_x,div_height);
            //ctx.drawImage(image,0,0,old_width,old_height,-(scale_x-div_width)/2,0,scale_x,div_width);
        }



    }


    hidden(){

    }

}